var db = require('./db.js');
var moment  = require('moment');

var onOffCache=[];
var gpsUniqSelect = function () {
    db.sequelize.query('select gu.unificationcode ,gu.g_time from T_GPSINFO_UNIQ gu'
        , { type: db.sequelize.QueryTypes.select })
        .then(function (results){
            onOffCache = results[0];
            //console.log(results[0]);
        });
}

var gpsUniqUpdate = function (unificationcode) {
    db.sequelize.query('update T_GPSINFO_UNIQ gu set gu.onoff = 0, gu.speed = 0 where gu.unificationcode = '+ unificationcode)
        .then(function (results){
            //console.log(results);
        });
}

var onOffJudge = function () {
    gpsUniqSelect();
    var now = new Date();
    for(var i = 0; i < onOffCache.length; i++){
        var diff = now.getTime() - onOffCache[i].g_time.getTime(); //单位为ms
        var diff_min = diff/1000/60; //单位为min
        //console.log(diff);
        if(diff_min > 5){
            gpsUniqUpdate(onOffCache[i].unificationcode);
        }
    }
}
setInterval(onOffJudge,1000);//1000为1秒钟

/*
 定时器判断上下线
 */